import random
import pygame
from config import *


def add(a, b):
    return list(map(lambda x, y: x + y, a, b))


def sub(a, b):
    return list(map(lambda x, y: x - y, a, b))


def multiple(a, b):
    return list(map(lambda x: x * b, a))


def generate_random_point() -> list:
    from config import width,height,grid_width
    print(width, height, grid_width)
    return [random.randrange(1, width // grid_width) * grid_width,
            random.randrange(1, height // grid_width) * grid_width]


def init():
    snake_head = generate_random_point()
    print(snake_head)
    tmp = {
        "UP": snake_head[1],
        "DOWN": height - snake_head[1],
        "LEFT": snake_head[0],
        "RIGHT": width - snake_head[0]
    }
    tmp2 = sorted(tmp.items(), key=lambda s: s[1], reverse=True)
    print(tmp2)
    
    direction = tmp2[0][0]
    print(direction)
    
    for i in range(0, snake_len, 1):
        snake_body.append(sub(snake_head, multiple(directions[direction], grid_width * i)))
        print(snake_body)
    return direction, snake_body


def generate_food():
    food_pos = generate_random_point()
    food_spawn = True
    return food_pos, food_spawn


def in_the_area(point, p1, p2, snake_speed) -> bool:
    if point[0] < p1[0] or point[0] > p2[0] - snake_speed:
        return False
    if point[1] < p1[1] or point[1] > p2[1] - snake_speed:
        return False
    return True


def rander_grid(screen, point1, point2):
    height = point2[1]
    width = point2[0]
    for i in range(point1[1], height, grid_width):
        pygame.draw.line(screen, (255, 255, 255), (0, i), (width, i))
    for i in range(point1[0], width, grid_width):
        pygame.draw.line(screen, (255, 255, 255), (i, 0), (i, height))


def show_message(screen):
    color = (255, 0, 0)
    font = pygame.font.Font(None, 3 * grid_width)
    text = font.render("Game Over", True, color)
    text_rect = text.get_rect(center=(screen.get_width() // 2, (screen.get_height() // 2)))
    
    # 创建一个白色的矩形Surface对象
    rect = pygame.Surface((width, height))
    rect.fill((255, 255, 255))
    rect.set_alpha(128)  # 设置矩形填充的透明度 0为完全透明，255为不透明
    screen.blit(rect, (0, 0))
    
    screen.blit(text, text_rect)


def manhattan_distance(p1, p2):
    return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])


def distance(p1, p2):
    return manhattan_distance(p1, p2)


def cheat_mode_get_direction(snake_head_pos, food_pos):
    import sys
    minimum_dis = sys.maxsize
    next_direction = list(directions.keys())[0]
    for selected_direction in directions:
        if selected_direction == "NONE":
            continue
        
        next_position = add(snake_head_pos, multiple(directions[selected_direction], grid_width))
        if not in_the_area(next_position, (0, 0), (width, height), grid_width):
            continue
        from config import snake_body
        if next_position in snake_body:
            continue
        
        # print(next_position)
        dis = distance(next_position, food_pos)
        if dis < minimum_dis:
            minimum_dis = dis
            next_direction = selected_direction
    print(next_direction)
    return next_direction


pygame.font.init()
font = pygame.font.SysFont("SimHei", 24)
font_path = r"C:\Windows\Fonts\STKAITI.TTF"
font_size = 24
font = pygame.font.Font(font_path, font_size)
screen = pygame.display.set_mode((800, 600))

# 定义按钮的外观
button_width = 200
button_height = 50
button_inactive_color = (200, 200, 200)
button_active_color = (255, 0, 0)


def draw_button(screen, rect, color, text):
    pygame.draw.rect(screen, color, rect)
    text_surface = font.render(text, True, (0, 0, 0))
    text_rect = text_surface.get_rect(center=rect.center)
    screen.blit(text_surface, text_rect)
